//
//  FeedbackView.swift
//  taxiDriver
//
//  Created by 恒  王 on 2023/12/9.
//

import SwiftUI
import AlertToast

struct FeedbackView: View {
    @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
    @State private var feedbackText = ""
    @State private var isEditing = false
    
    @State private var show = false
    @State private var errorShow = false
    @State private var errorStr = ""
    @State private var successShow = false
    var body: some View {
        NavigationView {
            GeometryReader { geometry in
                
                VStack (alignment: .leading){
                    ZStack(alignment: .topLeading) {
                        TextEditor(text: $feedbackText)
                            .frame(height: 450)
                            .cornerRadius(8)
//                            .overlay(
//                                RoundedRectangle(cornerRadius: 8)
//                                    .stroke(Color.gray, lineWidth: 1)
//                            )
                            .onTapGesture {
                                isEditing = true
                            }
                        if feedbackText.isEmpty && !isEditing {
                            Text("请填写意见和建议，便于我们改进，谢谢")
                                .foregroundColor(.gray)
                                .padding(.horizontal)
                        }
                    }
                   
                    Spacer()
                    Button(action: submitFeedback) {
                        Text("确定")
                            .frame(width: geometry.size.width - 30, height: 44)
                            .foregroundColor(Color.white)
                            .background(Color(hex: 0x85308D))
                            
                    }
                    .frame(width: geometry.size.width - 30, height: 44)
                    .cornerRadius(8)
                    .offset(y:-30)
                   
                }
                .frame(height: geometry.size.height)
                .padding()
                .onTapGesture {
                    UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
                }
                .toast(isPresenting: $show, alert: {
                    
                    return AlertToast(type: .loading, title: "")
                })
                .toast(isPresenting: $errorShow, alert: {
                    
                    if self.errorStr == "用户反馈内容不能为空" {
                        return AlertToast(type: .error(.red), title: String(localized: "用户反馈内容不能为空"))
                    } else {
                        
                        return AlertToast(type: .error(.red), title: self.errorStr)
                    }
                    
                })
                
                .toast(isPresenting: $successShow, alert: {
                    
                    return AlertToast(type: .regular, title: String(localized: "用户反馈成功"))
                }, completion:  {
                    presentationMode.wrappedValue.dismiss()
                })
            }
            
        }  .navigationBarHidden(false)
            .navigationBarBackButtonHidden(true) // 隐藏默认返回按钮
            .navigationBarItems(leading: backButton) // 添加自定义返回按钮
            .navigationBarTitle("用户反馈", displayMode: .inline)// 设置导航栏标题
    }
    
    var backButton: some View {
        Button(action: {
            presentationMode.wrappedValue.dismiss()
        }) {
            HStack {
                Image(systemName: "chevron.backward").foregroundColor(Color(hex: 0x999999))
            }
        }
    }
    func submitFeedback() {
        // 收回键盘
        UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
        self.saveFeedbackRequest()
    }
    
    func saveFeedbackRequest() {
        
        if self.feedbackText.isEmpty {
            
            self.errorStr = "用户反馈内容不能为空"
            self.errorShow.toggle()
            return
        }
        
        let openId = Singleton.shared.openId
      
    }
    
}

struct FeedbackView_Previews: PreviewProvider {
    static var previews: some View {
        FeedbackView()
    }
}
